<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :props="{
            children:'children',
            label:'manager'
          }"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="doAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="delIcm(data.id)"
                        >删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <el-dialog
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        :title="isEdit?'编辑部门':'新增部门'"
        :visible.sync="showDialog"
        width="50%"
        @closed="hClose"
      >
        <!-- 添加deptDialag组件 -->
        <!-- 接收子组件传过来的数据 -->
        <deptDialag
          :id="cutId"
          ref="deptRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="doSuccess"
          @close="showDialog=false"
        />
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="showDialog = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { delByDepart, getDepartments } from '@/api/departments'
import { tranDataToTreeData } from '@/utils'
// 将子组件引入
import deptDialag from './deptDialag.vue'
export default {
  // 注册子组件
  components: {
    deptDialag
  },
  data() {
    return {
      list: [],
      showDialog: false, // 控制弹窗的显示和隐藏
      cutId: '', // 将父组件的元素传给子组件
      isEdit: false, // false 新增 || true 编辑
      originList: []
    }
  },
  created() {
    this.addList()
  },
  methods: {
    async addList() {
      // 获取组织架构的部门列表
      try {
        const res = await getDepartments()
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 获取部门数据进行处理传个子组件
        this.originList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })
        res.data.depts.shift()
        // 将 数组进行树状转换
        this.list = tranDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 点击添加子部门
    doAdd(id) {
      this.showDialog = true
      // console.log(id)
      this.cutId = id
    },
    // 根据子组件传过来的数据重新渲染页面
    doSuccess() {
      // 重新渲染数据
      this.addList()
      // 关闭弹层
      this.showDialog = false
      this.isEdit = false
    },
    hEdit(id) {
      this.cutId = id
      this.isEdit = true // 编辑状态
      // 保证dom数据更新后，在调用子组件中的方法，重新获取数据并填充表单
      this.$nextTick(() => {
        this.$refs.deptRef.loadByDepart()
      })
      this.showDialog = true
    },
    // 删除部门
    delIcm(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
        // 发送删除请求
        const res = await delByDepart(id)
        console.log(res)
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.addList()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 关闭弹窗清空表单
    hClose() {
      // console.log('表单关闭了')
      this.$refs.deptRef.reseForm()
    }

  }
}
</script>
